package com.cn.yugan.Activity;

import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cn.yugan.Utils.DelectFileUtils;
import com.cn.yugan.Utils.Dialog.CustomDialog;
import com.cn.yugan.mobile.R;
import com.squareup.picasso.Picasso;

import java.io.File;


/**
 * Created by Rover on 2016/7/31 .
 * Email 357319517@qq.com
 */
public class SettingsActivity extends AppCompatActivity implements View.OnClickListener {

    private RelativeLayout rloptimsedimg;
    private RelativeLayout rlnoneimg;

    private CheckBox cboptimsedimg;
    private CheckBox cbnoneimg;

    private TextView tvclearimg;
    private TextView tvclearsearch;
    private TextView tvcheckupdate;
    private TextView tvattention;
    private String[] imgmode = {"仅移动网络环境无图", "仅WIFI网络环境无图", "全局无图"};
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        init();

    }

    private void init() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("设置");
        actionBar.setDisplayHomeAsUpEnabled(true);

        sp = getSharedPreferences("config", MODE_PRIVATE);

        rloptimsedimg = (RelativeLayout) findViewById(R.id.setting_optimised_img_rl);
        rlnoneimg = (RelativeLayout) findViewById(R.id.setting_noneimg_rl);
        cboptimsedimg = (CheckBox) findViewById(R.id.setting_optimised_img_cb);
        cbnoneimg = (CheckBox) findViewById(R.id.setting_noneimg_cb);
        tvclearimg = (TextView) findViewById(R.id.setting_clear_nativeimg_tv);
        tvclearsearch = (TextView) findViewById(R.id.setting_clear_nativesearch_tv);
        tvcheckupdate = (TextView) findViewById(R.id.setting_checkupdate_tv);
        tvattention = (TextView) findViewById(R.id.setting_attention_tv);

        tvattention.setOnClickListener(this);
        rlnoneimg.setOnClickListener(this);
        cboptimsedimg.setOnClickListener(this);
        cbnoneimg.setOnClickListener(this);
        tvclearimg.setOnClickListener(this);
        tvclearsearch.setOnClickListener(this);
        tvcheckupdate.setOnClickListener(this);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
                break;
        }
        return true;
    }

    @Override
    public void onClick(View v) {
        final CustomDialog.Builder builder = new CustomDialog.Builder(this);
        switch (v.getId()) {
            case R.id.setting_optimised_img_cb:
                //图片优化逻辑
                break;
            case R.id.setting_noneimg_rl:
                builder.setRadioGroup("仅移动网络环境无图");
                builder.setTitle("选择模式");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        //设置你的操作事项
                        int mode = 1;
                        SharedPreferences.Editor editor = sp.edit();
                        switch (builder.getRadioGroupinfo()) {
                            case "仅移动网络环境无图":
                                mode = 1;
                                break;
                            case "仅WIFI网络环境无图":
                                mode = 2;
                                break;
                            case "全局无图":
                                mode = 3;
                                break;

                        }
                        editor.putInt("load_pic_mode", mode);
                        editor.commit();
                        Toast.makeText(SettingsActivity.this, builder.getRadioGroupinfo(), Toast.LENGTH_SHORT).show();
                    }
                });

                builder.setNegativeButton("取消",
                        new android.content.DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });

                builder.create().show();
                break;
            case R.id.setting_noneimg_cb:
                //开启无图模式
                SharedPreferences.Editor editor = sp.edit();
                if (cbnoneimg.isChecked()) {

                    editor.putInt("nonpic", 1);
                    Toast.makeText(SettingsActivity.this, "鱼干娘为你开启了无图模式", Toast.LENGTH_SHORT).show();
                } else {
                    editor.putInt("nonpic", 0);
                    Toast.makeText(SettingsActivity.this, "鱼干娘为你关闭启了无图模式", Toast.LENGTH_SHORT).show();
                }
                editor.commit();
                break;
            
            case R.id.setting_clear_nativeimg_tv:
                builder.setMessage("确定要清空本地图片吗？清除内容包含已下载图片");
                builder.setTitle("注意");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        File file = new File(getString(R.string.imageuri));
                        DelectFileUtils.delectInnerDir(file);
                        Toast.makeText(SettingsActivity.this, "鱼干娘为你清空了本地图片哟", Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setNegativeButton("取消",
                        new android.content.DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                builder.create().show();
                break;
            case R.id.setting_clear_nativesearch_tv:
                builder.setMessage("确定要清空本地搜索记录吗？");
                builder.setTitle("注意");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        //设置你的操作事项
                        Toast.makeText(SettingsActivity.this, "鱼干娘为你清空了本地搜索记录哟，放心吧，我没有偷看的(捂脸偷笑)", Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setNegativeButton("取消",
                        new android.content.DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                builder.create().show();
                break;
            case R.id.setting_checkupdate_tv:
                //版本更新
                Toast.makeText(SettingsActivity.this, "您现在已经是最新版了", Toast.LENGTH_SHORT).show();
                break;
            case R.id.setting_attention_tv:
                builder.setWebview("file:///android_asset/agreement.html");
                builder.setTitle("用户须知");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.setNegativeButton("取消",
                        new android.content.DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                builder.create().show();
                break;
        }
    }
}
